package com.android.lovingpet.mine.setting.accountsecurity;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.lovingpet.R;
import com.android.lovingpet.base.BaseFragment;
import com.android.lovingpet.util.Utility;

public class EmailFragment extends BaseFragment<IAuthContract.View, AuthPresenter> implements View.OnClickListener, IAuthContract.View {

    private LinearLayout llEmail;
    private TextView tvGone;
    private EditText etEmail;
    private IAuthContract.Presenter presenter;

    @Override
    protected int setContentView() {
        return R.layout.fragment_email;
    }

    @Override
    protected void onCreateView(View view) {
        llEmail = view.findViewById(R.id.ll_email);
        tvGone = view.findViewById(R.id.tv_gone);
        etEmail = view.findViewById(R.id.et_email);
        presenter = getPresenter();
        view.findViewById(R.id.bt_send).setOnClickListener(this);
        ((TextView) view.findViewById(R.id.tv_title)).setText("邮箱验证");
        if (!TextUtils.isEmpty(Utility.getAccount().getEmail())) {
            setView();
        }
    }

    @Override
    public AuthPresenter createPresenter() {
        return new AuthPresenter(this);
    }

    @Override
    public IAuthContract.View createView() {
        return this;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_send:
                String email = etEmail.getText().toString().trim();
                if (TextUtils.isEmpty(email)) {
                    showToast("请输入电子邮箱地址");
                } else if (!email.contains("@")) {
                    showToast("请输入正确的电子邮箱地址");
                } else {
                    presenter.sendEmail(email);
                }
                break;

            default:
                break;
        }
    }

    @Override
    public void showToast(final String meg) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getContext(), meg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void setView() {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                llEmail.setVisibility(View.GONE);
                tvGone.setText("已发送验证邮件\n请检查邮箱");
                tvGone.setVisibility(View.VISIBLE);
            }
        });
    }

}
